Imports System.Reflection
Imports System.Xml
Imports Softelvdm.Controls
Imports Softelvdm.SftTreeNET

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim doc As XmlDocument = New XmlDocument()
    doc.Load("..\\..\\XmlFile.xml")

    SftTree1.Headers(0, 0).Text = "Options"
    sftTree1.RowHeaders.Width = 0
    ProcessNodes(doc.ChildNodes, Nothing)

    sftTree1.Columns.MakeOptimal(0, False)
    sftTree1.RecalcHorizontalExtent()
End Sub

Private Sub ProcessNodes(ByVal xmlNodeList As XmlNodeList, ByVal item As ItemClass)
    ' Process all nodes (recursively if necessary)
    For Each elem As XmlNode In xmlNodeList
        Dim id As String = ""
        Dim desc As String = ""
        Try
            id = elem.Attributes("ID").Value
            desc = elem.Attributes("Desc").Value
        Catch
            id = desc = ""
        End Try
        If id <> "" And desc <> "" Then
            Dim newItem As ItemClass = ProcessNode(item, elem, id, desc)
            If Not elem.ChildNodes Is Nothing Then
                ProcessNodes(elem.ChildNodes, newItem)
            End If
        Else
            If Not elem.ChildNodes Is Nothing Then
                ProcessNodes(elem.ChildNodes, item)
            End If
        End If
    Next
End Sub

' Process a node, looking for checkboxes and radiobuttons, everyhing else 
' is a section title
Private Function ProcessNode(ByVal item As ItemClass, ByVal elem As XmlNode, ByVal id As String, ByVal desc As String) As ItemClass
    Dim newItem As ItemClass
    If Not item Is Nothing Then
        newItem = item.Add()
    Else
        newItem = SftTree1.ItemCollection.Add()
    End If
    newItem.TagObject = id
    If elem.Name = "Checkbox" Then
        Dim check As CheckBoxStateEnum = CheckBoxStateEnum.Unchecked
        Dim strcheck As String = ""
        Try
            strcheck = elem.Attributes("Status").Value
        Catch
            strcheck = ""
        End Try
        If (strcheck <> "") Then
            If strcheck <> "0" Then
                check = CheckBoxStateEnum.Checked
            Else
                check = CheckBoxStateEnum.Unchecked
            End If
        End If
        newItem.Cells(0).Parts.Add(New CheckBoxPartClass(check))
    ElseIf elem.Name = "Radiobutton" Then
        Dim check As RadioButtonStateEnum = RadioButtonStateEnum.Unchecked
        Dim strcheck As String = ""
        Try
            strcheck = elem.Attributes("Status").Value
        Catch
            strcheck = ""
        End Try
        If strcheck <> "" Then
            If strcheck <> "0" Then
                check = RadioButtonStateEnum.Checked
            Else
                check = RadioButtonStateEnum.Unchecked
            End If
        End If
        newItem.Cells(0).Parts.Add(New RadioButtonPartClass(check))
    End If
    newItem.Cells(0).Text = desc
    Return newItem
End Function

Private Sub SftTree1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles SftTree1.KeyPress
    If e.KeyChar = " " Then
        Dim item As ItemClass = SftTree1.FocusItem
        If Not item Is Nothing Then
            Dim gp As GenericPartClass = item.Cells(0).Parts(0)
            If TypeOf gp Is CheckBoxPartClass Then
                Dim cb As CheckBoxPartClass = gp
                If cb.State = CheckBoxStateEnum.Checked Then
                    cb.State = CheckBoxStateEnum.Unchecked
                Else
                    cb.State = CheckBoxStateEnum.Checked
                End If
                e.Handled = True
                ' changing the checkbox state doesn't automatically call the CheckBoxClicked event
                Dim owningCell As CellClass = cb.PartOwner
                Dim owningItem As ItemClass = owningCell.OwningItem
                SftTree1_CheckBoxClicked(Me, New PartEventArgs(owningItem, owningCell, Nothing, cb))
            ElseIf TypeOf gp Is RadioButtonPartClass Then
                Dim rb As RadioButtonPartClass = gp
                If SftTree1.PerformClick_RadioButtonPart(rb) Then
                    e.Handled = True
                    ' changing the radiobutton state doesn't automatically call the RadioButtonClicked event
                    Dim owningCell As CellClass = rb.PartOwner
                    Dim owningItem As ItemClass = owningCell.OwningItem
                    SftTree1_RadioButtonClicked(Me, New PartEventArgs(owningItem, owningCell, Nothing, rb))
                End If
            End If
        End If
    End If
End Sub

Private Sub SftTree1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SftTree1.KeyDown
    If e.KeyCode = Keys.Space Then
        e.Handled = True
    End If
End Sub

Private Sub SftTree1_CheckBoxClicked(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.PartEventArgs) Handles SftTree1.CheckBoxClicked
    Debug.Print("The checkbox labeled " + e.Cell.Text + " was clicked.")
End Sub

Private Sub SftTree1_RadioButtonClicked(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.PartEventArgs) Handles SftTree1.RadioButtonClicked
    Debug.Print("The radiobutton labeled " + e.Cell.Text + " was clicked.")
End Sub

' This is a small helper routine to show all properties and fields of an object
Private Sub DumpValues(ByVal o As Object)
    Dim api() As PropertyInfo = o.GetType().GetProperties()
    For Each pi As PropertyInfo In api
        Dim s As String
        On Error Resume Next
        pi.GetValue(o, New Object() {})
        Debug.Write(" " & pi.Name & " " & s)
    Next
    Dim afi() As FieldInfo = o.GetType().GetFields()
    For Each fi As FieldInfo In afi
        Dim t As Object = fi.GetValue(o)
        Dim s As String = "(null)"
        If Not t Is Nothing Then s = t.ToString()
        Debug.Write(" " & fi.Name & " " & s)
    Next
    Debug.WriteLine("")
End Sub

End Class